<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="articles"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      style="width: 100%;min-height: 600px;">
      <el-table-column prop="title" fixed label="标题"></el-table-column>
      <el-table-column prop="user.username" label="用户名"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间"></el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="small"> 查看 </el-button> &nbsp;||&nbsp;
          <router-link :to="'/article/update?id='+scope.row.id">
            <el-button type="text" size="small"> 更新 </el-button> &nbsp;||&nbsp;
          </router-link>
          <el-button type="text" size="small" @click.native.prevent="deleteRow(scope, articles)"> 移除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, jumper, ->, total"
      :total="page.count"
      :page-size="page.pageSize"
      @current-change="changePage">
    </el-pagination>
  </div>
</template>

<script>
import { fetchList } from '@/api/article'

export default {
  name: 'ArticleIndex',
  data() {
    return {
      articles: [],
      page: {
        count: 0,
        pageSize: 1
      },
      loading: true
    }
  },
  watch: {
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    this.articleList()
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    async articleList(page) {
      this.loading = true
      const data = await fetchList(page)

      this.articles = data.data.list
      this.page = data.data.page
      this.loading = false
    },
    deleteRow(idx, tableData) {
      console.log(idx)
      console.log(tableData)
      console.log(tableData[idx])
    },
    changePage(page) {
      this.articleList({page: page})
    }
  }
}
</script>

<style scoped>

</style>
